<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签名字">
          <a-input placeholder="请输入标签名字" v-decorator="['name', validatorRules.name]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标签排序">
          <a-input-number
            v-decorator="['sort', validatorRules.sort ]"
            placeholder="请输入标签排序"
            :min="1"
            :max="999"
            style="width: 100%"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAdd, getUpdate } from '@/api/blog/blogTag';
import { Slime } from '@/mixins/slime';
export default {
  name: 'buildModal',
  mixins: [Slime],
  data() {
    return {
      title: '操作',
      model: {},
      form: this.$form.createForm(this),
      validatorRules: {
        name: { rules: [{ required: true, message: '请输入标签名字!' }] },
        sort: { rules: [{ required: true, message: '请输入标签排序!' }] },
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        //金钱转化
        this.form.setFieldsValue({
          id: this.model.id,
          name: this.model.name,
          sort: this.model.sort,
        })
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    //点击确定
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = {...values}
          //订购须知
          console.log(formData)
          if (!this.model.id) {
            getAdd(formData)
            .then((res) => {
              if (res.success) {
                that.$message.success('提交成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
          } else {
            formData = {...formData, id:this.model.id}
            getUpdate(formData)
            .then((res) => {
              if (res.success) {
                that.$message.success('提交成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        }
      })
    },
    //关闭
    handleCancel() {
      this.close()
    },
  }, 
}
</script>

<style lang="less" scoped>
</style>